<?php
	include_once("../backend/dataprovider.php");
	session_start();
	if( !isset($_SESSION["loggedUser"]) || $_SESSION["loggedUser"]==null){
		header("Location:../index.php");
		return;
	}
	if(isset($_GET["txtUsername"]))
		$sql = "select m.id, filename, mediatype, m.createdDate, url, a.id as articleId, u.username,m.active 
				from media as m left join users as u on m.userId = u.id
				left join articles a on a.id = m.article
			where filename like '%" . $_GET["txtUsername"] . "%'";
	else
		$sql = "select m.id, filename, mediatype, m.createdDate, url, a.id as articleId, u.username,m.active 
			from media as m left join users as u on m.userId = u.id
			left join articles a on a.id = m.article";
?>
<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="admin.css"/>
		<script type='text/javascript' src='../jquery-min.js'></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#btnSearch").click(function(){
					$("#frmSearchUser").submit();
				});
				
				$("#btnReg").click(function(){
					window.location.replace("reg.php");
					/*
						OR we can use
						window.location.href = "";
					*/
				});
				
				$("#btnLogout").click(function(){
					$.ajax({
						url:"backend/user.php",
						cache:false,
						type:'POST',
						data:{ACTION:'LOGOUT'},
						success:function(responseText){
							//alert(responseText);
							
							var rs = $.parseJSON(responseText);
							$('#div_user_pane').fadeOut();
							$('#div_login').fadeIn();
							
						}
					});
					return false;
				});
				
				$("#btnPost").click(function(){
					var tdata = new FormData();
					var file = document.getElementById("fAttach").files[0];
					
					tdata.append("ACTION", "UPLOADFILE");
					tdata.append("attach", file);
					
					$.ajax({
						url:"backend/post.php",
						cache:false,
						type:"POST",
						contentType:false,
						data:tdata,
						processData:false,
						success:function(responseText){
							alert(responseText);
							
							var rs = $.parseJSON(responseText);
							if(rs.status == "OK"){
								
							}else{
								alert(rs.status);
							}
							
						}
					});
					return false;
				});
			});
			
		</script>
		
		<title>VTech blog - A technical blog</title>
	</head>
	<body>
		<div id="container">
			<div id="div_header">
				<div id="div_user_pane">
					<span style="padding-left:30px;color:#dafafa;">Xin chao </span>
					<a href="#" style="text-decoration:none;color:#ffff22;" ><span style="font-weight:bold;"><?php echo $_SESSION["loggedUser"]; ?></span></a>
					<a href="#" style="text-decoration:none;color:#ffdada" id="btnLogout">Logout</a>
				</div>
			</div>
			<div id="div_nav" class="menu">
				<ul>
					<li><a href="../index.php">Home</a></li>
					<li><a href="../funct.php">NMath</a></li>
					<?php
						if(isset($_SESSION["loggedUser"])){
							if($_SESSION["loggedUser"]=='davu' || $_SESSION["loggedUser"]=='admin'|| $_SESSION["loggedUser"]=='huong'){
								print "<li><a href='dashboard.php'>Administrative</a></li>";
							}
						}
					?>
					<li><a href="../about.php">About me</a></li>
				</ul>
			</div>
			<div id="sidebar">
				<ul style="list-style-type:none">
					<li><a href="dashboard.php">Dashboard</a></li>
					<li><a href="userlst.php">Users</a></li>
					<li><a href="articles.php">Posts</a></li>
					<li>Files</li>
					<li><a href="../about.php">Application</a></li>
				</ul>
			</div>
			<div id="div_main">
				<div>
					<form id="frmSearchUser" name="frmSearchUser" action="articles.php">
						<input type="text" name="txtUsername" class="searchText" />
						<input type="submit" name="btnSearch" value="Search"/>
					</form>
				</div>
				<table class="tbl-data">
					<thead>
						<tr>
							<th>No.</th>
							<th class="tdlong">Filename</th>
							<th class="tdlong">MediaType</th>
							<th class="tdlong">Posted Date</th>
							<th class="tdlong">URL</th>
							<th class="tdlong">Article</th>
							<th class="tdlong">Author</th>
							<th>Active</th>
						</tr>
					</thead>
					<tbody>
				<?php
					$postLst = DataProvider::executeQuery($sql);
					if($postLst != false){
						$idx = 1;
						while($row = $postLst->fetch_assoc()){ ?>
							<tr>
								<td><?php print $idx; ?></td>
								<td class="tdlong"><a href="newpost.php?id=<?php print $row["id"]; ?>"><?php print $row["filename"]; ?></a></td>
								<td><?php print $row["mediatype"]; ?></td>
								<td class="tdlong"><?php print $row["createdDate"]; ?></td>
								<td class="tdlong"><?php print $row["url"]; ?></td>
								<td class="tdlong"><?php print $row["articleId"]; ?></td>
								<td><?php print $row["username"]; ?></td>
								<td><input type="checkbox" name="actives" value="yes" <?php print ($row["active"]==1)?"checked":""; ?>></td>
							</tr>
						<?php $idx++; }
					}
				?>
				</tbody>
				</table>
				<div>
					<input type="file" style="width:100%" id="fAttach" name="fAttach"/>
					<input type="button" id="btnPost" name="btnPost" value="Upload"></input>
				</div>
			</div>
			<div id="div_footer" >Copyright 2013 @Dao Anh Vu</div>
		</div>
	</body>
</html>
